﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Utility.DBMS;
using System.Utility.Messaging;
using System.Utility.UI;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UI
{
    public partial class AllInvoice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                GridView1.Visible = false;
                PopulateStates();
            }

        }

        private void PopulateStates()
        {
            ddState.DataSource = DBHelper.GetDataset("GetStates");
            ddState.DataTextField = "State";
            ddState.DataValueField = "State";

            ddState.DataBind();
        }

        protected void btnGo_Click(object sender,EventArgs e)
        {
            try
            {
                #region TestCode Without DB
                //DataTable dt = new DataTable();

                //dt.Columns.AddRange(new DataColumn[5] { new DataColumn("Customer"), new DataColumn("Location"), new DataColumn("Tax"), new DataColumn("OrderDate"), new DataColumn("Total") });

                //dt.Rows.Add("John Hammond", "United States","$4","12/7/2015","$900");

                //dt.Rows.Add("Mudassar Khan", "India", "$4", "12/7/2015", "$900");

                //dt.Rows.Add("Suzanne Mathews", "France", "$4", "12/7/2015", "$900");

                //dt.Rows.Add("Robert Schidner", "Russia", "$4", "12/7/2015", "$900");

                //GridView1.DataSource = dt;

                //GridView1.DataBind();

                //GridView1.Visible = true;
                #endregion
                LoadGrid();
                GridView1.Visible = true;
            }
            catch (Exception ex)
            {
                MessageUtility.LogAndGetMessageCode(ex);
                PageUtility.RegisterClientScriptBlockByMessageId(this, "GE0001");
            }
            PageUtility.RefreshByType(new Control[] { ddState, CalenderUserControl1, CalenderUserControl });
        }

        private void LoadGrid()
        {
            GridView1.DataSource = DBHelper.GetDataset("GetInvoicesByDate", new object[] { ddState.Items[ddState.SelectedIndex].Value, Convert.ToDateTime(((TextBox)CalenderUserControl1.FindControl("TextBox1")).Text), Convert.ToDateTime(((TextBox)CalenderUserControl.FindControl("TextBox1")).Text) });
            GridView1.DataBind();
        }

        protected void chkSelectAll_Click(object sender, EventArgs e)
        {
            CheckBox ChkBoxHeader = (CheckBox)GridView1.HeaderRow.FindControl("chkSelectAll");
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkItem");
                if (ChkBoxHeader.Checked == true)
                {
                    ChkBoxRows.Checked = true;
                }
                else
                {
                    ChkBoxRows.Checked = false;
                }
            }
        }

        protected void chkItem_Click(object sender, EventArgs e)
        {
            CheckBox ChkBoxHeader = (CheckBox)GridView1.HeaderRow.FindControl("chkSelectAll");
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox chk = (CheckBox)(row.Cells[0].FindControl("chkItem"));

                if (!chk.Checked && ChkBoxHeader.Checked)
                {
                    ChkBoxHeader.Checked = false;
                }
            }
        }

        protected void grdData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            LoadGrid();
        }
    }
}